/****************************************************************************
 * Copyright (C) 2011 Maxim Integrated Products, All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated Products
 * shall not be used except as stated in the Maxim Integrated Products
 * Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products retains all ownership rights.
 *
 *     Description: Declares I2C Library Function interfaces
 *             $Id: maxq_i2c.h 4465 2012-07-17 15:25:19Z jeremy.brodt $
 *
 ********************************************************************************
 */

/** 
* \file
* \brief    Inter-integrated circuit (IIC) communications interface driver.
* \details  This file defines the driver API including definitions, data types
*           and function prototypes.
* \details  This driver has configurations in maxq_config.h
*/

#ifndef _MAXQ_I2C_H_
#define _MAXQ_I2C_H_

#include "maxq_config.h"

/***** Definitions *****/

// I2C register bit mask definitions
/// \cond
#define I2C_ENABLE_MASK         0x0001 // I2CCN.0:I2CEN bit mask
#define I2C_MASTER_RECV_MODE    0x0004 // I2CCN.2:I2CMODE bit mask
/// \endcond

/// \brief I2C Speed Modes
typedef enum {
  I2C_STD_MODE,
  I2C_FAST_MODE,
  I2C_HS_MODE
} i2c_speed_t;


/***** Function Prototypes *****/

/**
 * \brief   Returns I2C Library Version information
 * \return  version number
 */
int i2c_version(void);

/**
 * \brief   Initialize I2C in master mode
 * \param   i2cspeed  desired speed (I2C mode)
 * \returns #MAXQ_SUCCESS if everything is successful, #MAXQ_FAILURE otherwise.
 */
int i2c_master_init(i2c_speed_t i2cspeed);

/// Enables the I2C interface
#define i2c_enable()    I2CCN|=I2C_ENABLE_MASK

/// Disables the I2C interface
#define i2c_disable()   I2CCN&=~I2C_ENABLE_MASK

/**
* \brief   Sends data to the slave address on the I2C bus.
* \param   address  slave address to which the byte will be sent
* \param   length   number of bytes to send
* \param   data     pointer to data to send
* \return  #MAXQ_FAILURE if the device was not ready, #MAXQ_SUCCESS otherwise.
*/
int i2c_master_write(uint16_t address, int length, uint8_t * data);

/**
* \brief   Sends one byte on the I2C bus.
* \param   data     byte of data to send
* \note    This function does not generate start/stop conditions.
* \return  #MAXQ_FAILURE if the device was not ready, #MAXQ_SUCCESS otherwise.
*/
int i2c_master_send_byte(uint8_t data);

/**
* \brief  Sets I2C bus in start condition
*/
int i2c_master_start(void);

/**
* \brief  Sets I2C bus in stop condition
*/
int i2c_master_stop(void);

/**
 * \brief   Receives data from slave
 * \param   address slave address
 * \param   length  number of bytes to receive
 * \param   data    pointer to buffer to store received data
 * \returns #MAXQ_FAILURE if the device was not ready, #MAXQ_SUCCESS otherwise.
 */
int i2c_master_read(uint16_t address, int length, uint8_t *data);

/**
 * \brief   Initialize I2C in slave mode
 * \param   address slave address to assume
 */
void i2c_slave_init(uint16_t address);

/**
*   \brief    Waits for reception of data from I2C Master
*   \returns  received data
*/
uint8_t i2c_slave_recv(void);

/**
*   \brief    Sends one byte data upon I2C Master request
*   \param    data  data byte to send
*   \returns  Resulting acknowledgment state. #TRUE if ACK, #FALSE if NACK.
*/
int i2c_slave_send(uint8_t data);

/**
*   \brief   Configures acknowledgment value (ACK or NACK)
*   \param   ackval #TRUE if ACK, #FALSE if NACK.
*/
void i2c_acknowledge(int ackval);

#endif /* _MAXQ_I2C_H_ */
